package com.base.api.pms;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.base.dao.pms.PmsPublicQeuryMapper;
import com.base.vo.thbi.InsuranceVsPriceSychi;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模板方法
 */
@Component
public  class PmsPublicSqlQuery {

    @Autowired
    private PmsPublicQeuryMapper pmsPublicQeuryMapper;

    public String splicSql(String sql, Map<String, Object> param){
        return sql;
    };

    public Map<String, Object> beforReturn(Map<String, Object> map) {
        return map;
    }

    public Map<String, Object> ExecuteQuery(String sql, Map<String, Object> map) {
        String sql1 = splicSql(sql, map);
        List<Map<String, Object>> list = pmsPublicQeuryMapper.selectPublicItemList(sql1);
        Map<String, Object> map1 = new HashMap<>();
        map1.put("Data", list);
        return beforReturn(map1);
    }

    public Map<String, Object> ExecuteQueryByPageHelper(String sql, Map<String, Object> param) {
        Map<String,Object> pageRequest = (HashMap<String, Object>)(param.get("PageRequest"));
        String sql1 = splicSql(sql, (HashMap<String, Object>)(param.get("params")));
        int PageIndex = 1;
        int PageSize = 10;
        if(pageRequest.get("PageIndex") !=null){
            PageIndex = (int) (pageRequest.get("PageIndex"));
        }
        if(pageRequest.get("PageSize") !=null){
            PageSize = (int) (pageRequest.get("PageSize"));
        }
        PageHelper.startPage(PageIndex, PageSize);
        List<Map<String, Object>> list = pmsPublicQeuryMapper.selectPublicItemList(sql1);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
        Map<String, Object> map1 = new HashMap<>();
        map1.put("Data", pageInfo.getList());
        map1.put("TotalCount", pageInfo.getTotal());
        map1.put("PageSize", pageInfo.getPageSize());
        map1.put("PageIndex", pageInfo.getPageNum());
        return beforReturn(map1);
    }


}
